package org.stvd.controller.admin;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.security.SecurityUserHolder;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.common.utils.ServiceResult;
import org.stvd.controller.BaseController;
import org.stvd.entities.admin.UserLogin;
import org.stvd.service.admin.UsersService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 用户账号绑定-控制类
 * @author houzx
 */
@Api(tags = "账号绑定接口类")
@Controller
@RequestMapping("admin/binding")
public class UserBindController extends BaseController{

    @Autowired
    private UsersService usersService;
    
    private String tmpURL = "/admin/binding/";
    
    /**
     * 手机号绑定
     * @return
     */
    @ApiIgnore
    @GetMapping(value="mobile")
    public String UserMobileBindload() {
        return tmpURL+ "UserMobileBinding.html";
    }
    
    @ApiOperation(value = "绑定手机号码")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "手机号码", name="mobile_num", required = true, paramType = "query"),
        @ApiImplicitParam(value = "验证码", name="validate_code", required = true, paramType = "query")
    })
    @Log(title = "绑定手机号码", businessType = BusinessType.UPDATE)
    @PostMapping(value="mobile")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> UserMobileBindAction() {
        InterfaceResult<Map<String, Object>> interfaceResult = new InterfaceResult<>();
        String mobileStr = request.getParameter("mobile_num");
        String validateCode = request.getParameter("validate_code");
        String userId = SecurityUserHolder.getCurrentUserId();
        ServiceResult<Map<String, Object>> serviceResult = usersService.bindingUserMobile(userId, mobileStr, validateCode);
        if(serviceResult.isSuccess()) {
            List<UserLogin> userLoginList = usersService.findUserLoginByUserId(userId);
            SecurityUserHolder.getCurrentUserInfo().setUserLoginList(userLoginList);
            interfaceResult.setMsg(serviceResult.getMessage());
        }else {
            interfaceResult.setError(serviceResult.getMessage());
        }
        return interfaceResult;
    }
    
    /**
     * 电子邮箱绑定
     * @returnØß
     */
    @ApiIgnore
    @GetMapping(value="email")
    public String UserEmailBindload() {
        return tmpURL + "UserEmailBinding.html";
    }
    
    @ApiOperation(value = "绑定电子邮箱")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "电子邮箱", name="email_num", required = true, paramType = "query"),
        @ApiImplicitParam(value = "验证码", name="validate_code", required = true, paramType = "query")
    })
    @Log(title = "绑定电子邮箱", businessType = BusinessType.UPDATE)
    @PostMapping(value="email")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> UserEmailBindAction() {
        InterfaceResult<Map<String, Object>> interfaceResult = new InterfaceResult<>();
        String emailStr = request.getParameter("email_num");
        String validateCode = request.getParameter("validate_code");
        String userId = SecurityUserHolder.getCurrentUserId();
        ServiceResult<Map<String, Object>> serviceResult = usersService.bindingUserEmail(userId, emailStr, validateCode);
        if(serviceResult.isSuccess()) {
            List<UserLogin> userLoginList = usersService.findUserLoginByUserId(userId);
            SecurityUserHolder.getCurrentUserInfo().setUserLoginList(userLoginList);
            interfaceResult.setMsg(serviceResult.getMessage());
        }else {
            interfaceResult.setError(serviceResult.getMessage());
        }
        return interfaceResult;
    }
}
